package edu.example;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.TreeMap;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class selectFieldsInViewTable extends JDialog
{	
	private Vector<String> selections;
	private ApplicationManagerCloud am;
	private UserManager um;
	
	private JCheckBox nameCheckBox;
	private JCheckBox emailCheckBox;
	private JCheckBox toeflCheckBox;
	private JCheckBox ieltsCheckBox;
	private JCheckBox greVerbalCheckBox;
	private JCheckBox greQuantitativeCheckBox;
	private JCheckBox greAnalyticalCheckBox;
	private JCheckBox institution1CheckBox;
	private JCheckBox institution2CheckBox;
	private JCheckBox undergraduateGpaCheckBox;
	private JCheckBox graduateGpaCheckBox;
	private JCheckBox programCheckBox;
	private JCheckBox indicateTopicCheckBox;
	private JCheckBox sbuIdCheckBox;
	private JCheckBox sbuGpaCheckBox;
	private JCheckBox tierCheckBox;
	
	private JPanel mainPanel;
	private JPanel lowerButtonPanel;
	private JPanel upperButtonPanel; 
	
	private JButton backArrowButton;
	private JButton nextButton;
	
	
	// event Handler
	private NextHandler nextHandler;
	private BackHandler backHandler;
	
	public selectFieldsInViewTable(ApplicationManagerCloud initAm, UserManager initUm)
	{
		this.setTitle("Select Fields");
		this.setResizable(false);
		selections = new Vector<String>();
		am = initAm;
		um = initUm;
		layoutGUI();
	}
	
	private void layoutGUI() 
	{
		Container c = this.getContentPane();
		c.setBackground(Color.WHITE);
		mainPanel = new JPanel();
		lowerButtonPanel = new JPanel();
		upperButtonPanel = new JPanel();
		mainPanel.setBackground(Color.WHITE);
		lowerButtonPanel.setBackground(Color.WHITE);
		upperButtonPanel.setBackground(Color.WHITE);
		
		nameCheckBox = new JCheckBox("Name");
		emailCheckBox = new JCheckBox("E-Mail");
		toeflCheckBox = new JCheckBox("TOEFL");
		ieltsCheckBox = new JCheckBox("IELTS");
		greVerbalCheckBox = new JCheckBox("GRE Verbal");
		greQuantitativeCheckBox = new JCheckBox("GRE Quantitative");
		greAnalyticalCheckBox = new JCheckBox("GRE Analytical");
		institution1CheckBox = new JCheckBox("Institution1");
		institution2CheckBox = new JCheckBox("Institution2");
		undergraduateGpaCheckBox = new JCheckBox("Undergraduate GPA");
		graduateGpaCheckBox = new JCheckBox("Graduate GPA");
		programCheckBox = new JCheckBox("Program");
		indicateTopicCheckBox = new JCheckBox("Indicate Topic");
		sbuIdCheckBox = new JCheckBox("SBU ID");
		sbuGpaCheckBox = new JCheckBox("SBU GPA");
		tierCheckBox = new JCheckBox("Tier");
		
		
		nameCheckBox.setBackground(Color.WHITE);
		emailCheckBox.setBackground(Color.WHITE);
		toeflCheckBox.setBackground(Color.WHITE);
		ieltsCheckBox.setBackground(Color.WHITE);
		greVerbalCheckBox.setBackground(Color.WHITE);
		greQuantitativeCheckBox.setBackground(Color.WHITE);
		greAnalyticalCheckBox.setBackground(Color.WHITE);
		institution1CheckBox.setBackground(Color.WHITE);
		institution2CheckBox.setBackground(Color.WHITE);
		undergraduateGpaCheckBox.setBackground(Color.WHITE);
		graduateGpaCheckBox.setBackground(Color.WHITE);
		programCheckBox.setBackground(Color.WHITE);
		indicateTopicCheckBox.setBackground(Color.WHITE);
		sbuIdCheckBox.setBackground(Color.WHITE);
		sbuGpaCheckBox.setBackground(Color.WHITE);
		tierCheckBox.setBackground(Color.WHITE);
		
		ImageIcon icon = new ImageIcon("record_back.png");
		backArrowButton = new JButton(icon);
		nextButton = new JButton("Next");
		
		mainPanel.setLayout(new GridBagLayout());
		addComponent(nameCheckBox,				mainPanel,	1,	1,	1,	1);
		addComponent(emailCheckBox,				mainPanel,	2,	1,	1,	1);
		addComponent(toeflCheckBox,				mainPanel,	1,	2,	1,	1);
		addComponent(ieltsCheckBox,				mainPanel,	2,	2,	1,	1);
		addComponent(greVerbalCheckBox,			mainPanel,	1,	3,	1,	1);
		addComponent(greQuantitativeCheckBox,	mainPanel,	2,	3,	1,	1);
		addComponent(greAnalyticalCheckBox,		mainPanel,	1,	4,	1,	1);
		addComponent(institution1CheckBox,		mainPanel,	2,	4,	1,	1);
		addComponent(institution2CheckBox,		mainPanel,	1,	5,	1,	1);
		addComponent(undergraduateGpaCheckBox,	mainPanel,	2,	5,	1,	1);
		addComponent(graduateGpaCheckBox,		mainPanel,	1,	6,	1,	1);
		addComponent(programCheckBox,			mainPanel,	2,	6,	1,	1);
		addComponent(indicateTopicCheckBox,		mainPanel,	1,	7,	1,	1);
		addComponent(sbuIdCheckBox,				mainPanel,	2,	7,	1,	1);
		addComponent(sbuGpaCheckBox,			mainPanel,	1,	8,	1,	1);
		addComponent(tierCheckBox,				mainPanel,	2,	8,	1,	1);
		
		lowerButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		lowerButtonPanel.add(nextButton);
		
		upperButtonPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
		upperButtonPanel.add(backArrowButton);
		
		this.add(upperButtonPanel, BorderLayout.NORTH);
		this.add(mainPanel, BorderLayout.CENTER);
		this.add(lowerButtonPanel, BorderLayout.SOUTH);
		// size and locate the dialog at the center of the screen
		this.pack();
		this.setModal(true);
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
		
		nextHandler = new NextHandler();
		backHandler = new BackHandler();
		backArrowButton.addActionListener(backHandler);
		nextButton.addActionListener(nextHandler);
	}

	// method helping GridBagLayout
	public void addComponent(	JComponent jc, Container c, 
			int x, int y, int w, int h){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	
	public static void main (String[] args)
	{
		selectFieldsInViewTable temp = new selectFieldsInViewTable(null, null);
		temp.setVisible(true);
	}
	
	class NextHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			 for(int i = 0; i< mainPanel.getComponentCount(); i++)
			 {
				 Component[] a = mainPanel.getComponents();
				 JCheckBox tempBox = (JCheckBox) a[i];		 
				 if(tempBox.isSelected())
				 {
					 selections.add(tempBox.getText());
				 }
			 }
			 
			 ApplicationManagerCloud ap = ApplicationManagerCloud.getAppManager();
			 selectStudentsInViewTable sivt = new selectStudentsInViewTable(ap, um, ap.getAllApps(), selections);
			 sivt.setVisible(true);
			 
			 selectFieldsInViewTable.this.setVisible(false);
		}
		
	}
	class BackHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			selectFieldsInViewTable.this.setVisible(false);
		}
		
	}
}
